package com.weimei.conversionTreasure.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.activity.WebActivity;

import org.jetbrains.annotations.NotNull;

import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;


/**
 * 用户及隐私协议弹窗
 */

public class UserAgreementDialog extends Dialog {
    TextView tvContent;
    TextView disagree;
    TextView agree;
    CheckBox cb;

    private Context context;
    private onDialogClickListener mOnDialogClickListener;
    private boolean saveStatus = true; // 保存协议状态
    public static final String IS_CONFIRM_USER_AGREEMENT = "IS_CONFIRM_USER_AGREEMENT";

    public onDialogClickListener getOnDialogClickListener() {
        return mOnDialogClickListener;
    }

    public void setOnDialogClickListener(onDialogClickListener mOnDialogClickListener) {
        this.mOnDialogClickListener = mOnDialogClickListener;
    }

    public UserAgreementDialog(Context context) {
        super(context, R.style.AgreementDialog);
        this.context = context;
    }

    public UserAgreementDialog(Context context, onDialogClickListener listener) {
        super(context, R.style.AgreementDialog);
        this.context = context;
        this.mOnDialogClickListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(context, R.layout.dialog_user_layout, null);
        setCancelable(false);//点击外部不可dismiss
        setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        window.setContentView(view);
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = window.getAttributes();

        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        int width1 = display.getWidth() * 5 / 6; // 设置dialog宽度为屏幕的4/5

        params.width = width1;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        tvContent = view.findViewById(R.id.tv_content);
        initTextView("");

        cb = view.findViewById(R.id.cb);

        disagree = view.findViewById(R.id.disagree);
        disagree.setOnClickListener(v -> {
            SpUtils.getInstance().putBoolean(IS_CONFIRM_USER_AGREEMENT,false);
            if (mOnDialogClickListener != null) {
                mOnDialogClickListener.onCancel();
            }
            dismiss();
        });

        agree = view.findViewById(R.id.agree);
        agree.setOnClickListener(v -> {
            if (cb.isChecked()){
                SpUtils.getInstance().putBoolean(IS_CONFIRM_USER_AGREEMENT,true);
                if (mOnDialogClickListener != null) {
                    mOnDialogClickListener.onConfirm();
                }
                dismiss();
            }else {
                ToastUtils.showShort("请阅读并同意隐私政策和用户协议");
            }
        });

        view.findViewById(R.id.tv_ys).setOnClickListener(v -> {
            Intent intent = new Intent(context, WebActivity.class);
            intent.putExtra("flag", "YSZC");
            context.startActivity(intent);
        });

        view.findViewById(R.id.tv_yh).setOnClickListener(v -> {
            Intent intent = new Intent(context, WebActivity.class);
            intent.putExtra("flag", "yhxz");
            context.startActivity(intent);
        });
    }

    /**
     * 初始化webView
     */
    private void initTextView(String showText) {
        String text = context.getResources().getString(R.string.explain);

        //设置文字
        SpannableStringBuilder style = new SpannableStringBuilder();
        int result1, result2;
        if (!showText.equals("")) {
            result1 = showText.indexOf("《隐私政策》");
            result2 = showText.indexOf("《用户协议》");
            style.append(showText);
        } else {
            result1 = text.indexOf("《隐私政策》");
            result2 = text.indexOf("《用户协议》");
            style.append(text);
        }

        //隐私政策
        ClickableSpan PrivacyPolicy = new ClickableSpan() {
            @Override
            public void onClick(@NotNull View widget) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra("flag", "YSZC");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        //用户协议
        ClickableSpan ServiceAgreement = new ClickableSpan() {
            @Override
            public void onClick(@NotNull View widget) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra("flag", "yhxz");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        //隐私政策
        style.setSpan(PrivacyPolicy, result1, result1 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(ServiceAgreement, result2, result2 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(style);
        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#31A4FD"));
        ForegroundColorSpan foregroundColorSpan01 = new ForegroundColorSpan(Color.parseColor("#31A4FD"));
        style.setSpan(foregroundColorSpan, result1, result1 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(foregroundColorSpan01, result2, result2 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        RelativeSizeSpan span = new RelativeSizeSpan(1.18f);
        style.setSpan(span, result1, result1 + 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        RelativeSizeSpan span2 = new RelativeSizeSpan(1.18f);
        style.setSpan(span2, result2, result2 + 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


        //配置给TextView
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        tvContent.setText(style);
        tvContent.setHighlightColor(Color.parseColor("#00000000"));//去掉点击后的背景颜色为透明
    }

    /**
     * 设置按钮文字
     *
     * @param cancel  取消文字
     * @param confirm 同意文字
     * @param save    保存协议状态
     * @param hint    提示内容
     */
    public void setButtonText(String cancel, String confirm, boolean save, String hint) {
        disagree.setText(cancel);
        agree.setText(confirm);
        saveStatus = save;
        tvContent.setText(hint);
        initTextView(hint);
    }


    public interface onDialogClickListener {
        void onConfirm();

        void onCancel();
    }
}
